import { createRouter, createWebHashHistory } from 'vue-router'
import {mapState} from 'vuex'
import store from '../store/index.js'; // 导入 Vuex 实例
import api from '../api/index.js'

const routes = [
  {
    path: '/',
    name: 'home',
    redirect: '/user/login'
  },
  {
    path: '/user/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "login" */ '../views/user/Login.vue')
  },
  // {
  //   path: '/user/register',
  //   name: 'register',
  //   component: () => import(/* webpackChunkName: "about" */ '../views/Register.vue')
  // },
  {
    path: '/projectManager',
    name: 'projectManager',
    component: () => import(/* webpackChunkName: "projects" */ '../views/user/Projects.vue'),
	children: [
		{
			path: '/myProjects',
			name: 'myProject',
			component: () => import(/* webpackChunkName: "myProject" */ '../views/user/MyProject.vue'),
		},
		{
			path: '/project/list',
			name: 'projectList',
			component: () => import(/* webpackChunkName: "projectList" */ '../views/user/ProjectList.vue'),
		},
		{
			path: '/project/appeal',
			name: 'projectAppeal',
			component: () => import(/* webpackChunkName: "projectAppeal" */ '../views/user/ProjectAppeal.vue'),
		},
		{
			path: '/project/myAppeal',
			name: 'myAppeal',
			component: () => import(/* webpackChunkName: "myAppeal" */ '../views/user/ProjectMyAppeal.vue'),
		},
	]
  },
  {
	path: '/project',
	name: 'projectHome',
	component: () => import(/* webpackChunkName: "projectHome" */ '../views/ProjectHome.vue'),
	children: [
		{
			path: '/user/list',
			name: 'user',
			component: () => import(/* webpackChunkName: "user" */ '../views/user/User.vue'),
		},
		{
			path: '/user/role',
			name: 'role',
			component: () => import(/* webpackChunkName: "role" */ '../views/user/Role.vue'),
		},
		{
			path: '/user/roleCreate',
			name: 'roleCreate',
			component: () => import(/* webpackChunkName: "createEditRole" */ '../views/user/CreateEditRole.vue'),
		},
		{
			path: '/user/roleEdit',
			name: 'roleEdit',
			component: () => import(/* webpackChunkName: "createEditRole" */ '../views/user/CreateEditRole.vue'),
		},
		{
			path: '/user/permission',
			name: 'permission',
			component: () => import(/* webpackChunkName: "permission" */ '../views/user/Permission.vue'),
		},
		{
			path: '/project/index',
			name: 'index',
			component: () => import(/* webpackChunkName: "projectIndex" */ '../views/ProjectIndex.vue'),
		},
		{
			path: '/env/env',
			name: 'env',
			component: () => import(/* webpackChunkName: "env" */ '../views/env/Env.vue'),
		},
		{
			path: '/env/service',
			name: 'service',
			component: () => import(/* webpackChunkName: "service" */ '../views/env/Service.vue'),
		},
		{
			path: '/env/plant',
			name: 'plant',
			component: () => import(/* webpackChunkName: "plant" */ '../views/env/Plant.vue'),
		},
		{
			path: '/env/module',
			name: 'module',
			component: () => import(/* webpackChunkName: "module" */ '../views/env/Module.vue'),
		},
		{
			path: '/env/page',
			name: 'page',
			component: () => import(/* webpackChunkName: "page" */ '../views/env/Page.vue'),
		},
		{
			path: '/env/header',
			name: 'header',
			component: () => import(/* webpackChunkName: "header" */ '../views/env/Header.vue'),
		},
		{
			path: '/env/db',
			name: 'db',
			component: () => import(/* webpackChunkName: "db" */ '../views/env/Db.vue'),
		},
		{
			path: '/interface/api',
			name: 'api',
			component: () => import(/* webpackChunkName: "about" */ '../views/interface/Api.vue'),
		},
		{
			path: '/interface/apiCreate',
			name: 'apiCreate',
			component: () => import(/* webpackChunkName: "apiCE" */ '../views/interface/ApiCE.vue'),
		},
		{
			path: '/interface/apiEdit',
			name: 'apiEdit',
			component: () => import(/* webpackChunkName: "apiCE" */ '../views/interface/ApiCE.vue'),
		},
		{
			path: '/python/edit',
			name: 'python',
			component: () => import(/* webpackChunkName: "python" */ '../views/script/Python.vue'),
		},
		{
			path: '/test/case',
			name: 'case',
			component: () => import(/* webpackChunkName: "CaseList" */ '../views/case/CaseList.vue'),
		},
		{
			path: '/test/tag',
			name: 'tag',
			component: () => import(/* webpackChunkName: "TagList" */ '../views/case/TagList.vue'),
		},
		{
			path: '/test/caseCreate',
			name: 'caseCreate',
			component: () => import(/* webpackChunkName: "caseCE" */ '../views/case/CaseCE.vue'),
		},
		{
			path: '/element/list',
			name: 'element',
			component: () => import(/* webpackChunkName: "element" */ '../views/element/Element.vue'),
		},
		{
			path: '/test/caseEdit',
			name: 'caseEdit',
			component: () => import(/* webpackChunkName: "caseCE" */ '../views/case/CaseCE.vue'),
		},
		{
			path: '/test/caseCopy',
			name: 'caseCopy',
			component: () => import(/* webpackChunkName: "caseCE" */ '../views/case/CaseCE.vue'),
		},
		{
			path: '/test/step',
			name: 'step',
			component: () => import(/* webpackChunkName: "stepList" */ '../views/case/StepList.vue'),
		},
		{
			path: '/test/stepCreate',
			name: 'stepCreate',
			component: () => import(/* webpackChunkName: "stepCE" */ '../views/case/StepCE.vue'),
		},
		{
			path: '/test/stepEdit',
			name: 'stepEdit',
			component: () => import(/* webpackChunkName: "stepCE" */ '../views/case/StepCE.vue'),
		},
		{
			path: '/test/stepCopy',
			name: 'stepCopy',
			component: () => import(/* webpackChunkName: "stepCE" */ '../views/case/StepCE.vue'),
		},
		{
			path: '/suite/list',
			name: 'suite',
			component: () => import(/* webpackChunkName: "suiteList" */ '../views/suite/SuiteList.vue'),
		},
		{
			path: '/suite/task',
			name: 'task',
			component: () => import(/* webpackChunkName: "taskPlan" */ '../views/suite/TaskPlan.vue'),
		},
		{
			path: '/report/list',
			name: 'report',
			component: () => import(/* webpackChunkName: "reportList" */ '../views/report/ReportList.vue'),
		},
		{
			path: '/report/listView',
			name: 'reportViewDetail',
			component: () => import(/* webpackChunkName: "reportDetail" */ '../views/report/ReportDetail.vue'),
		},
	]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})


async function getUserPermission(id){
	const response = await api.getUser(id)
	if (response.status === 200){
		return response
	}
}


async function hasPermission(path){
	if (['/user/login', '/user/register', '/projectManager', '/project/index', '/myProjects', '/project/list', '/project/appeal', '/project/myAppeal'].includes(path)){
		return true
	}
	// let role_permissions = JSON.parse(window.localStorage.getItem('role_permissions'))
	// if(!role_permissions){
		
	// 	window.localStorage.setItem('role_permissions', JSON.stringify(role_permissions))
	// }
	const user_id = store.state.user_id
	const response =  await getUserPermission(user_id)
	const role_permissions = response.data.result.role_info.role_permissions
	const index = role_permissions.findIndex((item)=>{
		return path.includes(item.permission.path) && item.permission.level != 1
	})
	if(index === -1){
		return false
	}
	if(role_permissions[index].has_permission){
		return true
	}
	return false
}

// 设置路由导航守卫ie，控制前端路由访问的权限
router.beforeEach((to, from, next) =>{
	const token = window.localStorage.getItem('token')
	if(token){
		hasPermission(to.path).then(isHas =>{
			if(isHas){
				next()
			}else{
				next(from)
			}
		})
	}else{
		if (to.name === 'login'){
			next()
		}else{
			next({name: 'login'})
		}
		
	}
})
export default router
